/*
                  Profiles Class Implementation
*/
#include "Profiles.h"

vector<Profile*> Profiles::ProfileList = vector<Profile*>();

void Profiles::ClearProfiles()
{
    for (int i = 0; i < ProfileList.size(); i++)
    {
        delete ProfileList.at(i);
    }

    ProfileList.clear();
}

int Profiles::ReadFile(const string& strFilepath)
{
    ifstream profilesFile;

    profilesFile.open(strFilepath.c_str());

    string strLine;

    // If the config file couldn't be opened (most likely doesn't exist) return and write a new default file
    if (! profilesFile.is_open())
    {
        printf("Profiles Error: Couldn't open profiles file.\n");

        profilesFile.close();

        return -1;
    }

    ClearProfiles();

    while (! profilesFile.eof())
    {
        getline(profilesFile, strLine);

        if (strLine != "")
        {
            ProfileList.push_back(new Profile);

            ProfileList.back()->CreateFromString(strLine);
        }
    }

    profilesFile.close();

    return 0;
}

int Profiles::WriteFile(const string& strFilepath)
{
    ofstream profilesFile;

    profilesFile.open(strFilepath.c_str());

    for (int i = 0; i < ProfileList.size(); i++)
    {
        profilesFile << ProfileList.at(i)->getName() << ",";
        profilesFile << lexical_cast<string>(ProfileList.at(i)->getExperience());
        profilesFile << "\n";
    }

    return 0;
}
